package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

func main() {
	addr := ":9999"
	http.HandleFunc("/header/", func(w http.ResponseWriter, r *http.Request) {
		// 设置状态码
		var headers http.Header = w.Header()
		headers.Add("Server", "leleserver")
		headers.Set("xxx", "xxxxxxxxxxx")
		w.WriteHeader(http.StatusCreated)
		fmt.Fprintln(w, "headwears")
	})

	http.HandleFunc("/redirect/", func(w http.ResponseWriter, r *http.Request) {
		// 跳转 /home/
		http.Redirect(w, r, "/home/", http.StatusFound)
		// 响应 302
		// Location: /home/
		// 重新发起请求 /home/ GET
	})

	http.HandleFunc("/home/", func(w http.ResponseWriter, r *http.Request) {
		// 跳转 /home/
		fmt.Fprintln(w, "home")
	})

	http.HandleFunc("/json/", func(w http.ResponseWriter, r *http.Request) {
		rt := map[string]string{
			"a":   "111",
			"xxx": "xxx",
		}
		encoder := json.NewEncoder(w)
		encoder.Encode(rt)

	})

	log.Fatal(http.ListenAndServe(addr, nil))

}
